package cn.wolfcode.utils;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 杨兴健
 * @Date 2020/7/13 19:04
 */
public class UserContext {

    /**
     * 当前用户信息在session中的key
     */
    public static final String USER_IN_SESSION = "USER_IN_SESSION";

    /**
     * 获取session对象
     *
     * @return
     */
    public static HttpSession getSession() {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert attrs != null;
        return attrs.getRequest().getSession();
    }

    /**
     * 获取当前用户的信息
     *
     * @return
     */
    public static Employee getCurrentUser() {
        return (Employee) getSession().getAttribute(USER_IN_SESSION);
    }

    /**
     * 设置当前用户进session
     *
     * @param employee
     */
    public static void setUserInSession(Employee employee) {
        getSession().setAttribute(USER_IN_SESSION, employee);
    }
}
